home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 December / DPPCPRO1205.ISO / Essentials / Programming / Basic4GL / Setup Basic4GL v2.3.1.exe / $INSTDIR / Programs / Starfield2.gb < prev    next >
Encoding:
Text File  |  2005-07-29  |  1.7 KB  |  56 lines

  1. ' Star field demo 2
  2. '
  3. ' Varying star's brightness by explicitly setting the colour
  4.  
  5. ' Note:
  6. ' This demo is based on the source code from the previous one.
  7. ' I've removed most of the explanation comments from the previous demo, 
  8. ' and just commented the new pieces.
  9. '   * I've upped the number of stars, and doubled the movement speed also
  10.  
  11. const maxStars = 1000
  12.  
  13. dim stars#(maxStars)(2)
  14. dim i, intensity#
  15.  
  16. ' Populate star field
  17. for i = 1 to maxStars
  18.     stars#(i) = vec3 (rnd () % 401 - 200, rnd () % 401 - 200, -i)
  19. next
  20.  
  21. glDisable (GL_DEPTH_TEST)
  22.  
  23. ' Main loop
  24. while true    
  25.     glClear (GL_COLOR_BUFFER_BIT)       
  26.  
  27.     for i = 1 to maxStars
  28.     
  29.         ' Move the star forward, by adding 1 to Z
  30.         stars#(i) = stars#(i) + vec3 (0, 0, 2)
  31.  
  32.         ' If the Z goes positive (behind the screen), move it to the back again.
  33.         if stars#(i)(2) >= 0 then stars#(i)(2) = -maxStars endif
  34.         
  35.         ' Calculate the star's intensity. 
  36.         ' We want this to come out as a value between 0 and 1.
  37.         ' So the furthest away stars have intensity 0, and the closest have 1.
  38.         intensity# = (stars#(i)(2) + maxStars) / maxStars
  39.         glBegin (GL_POINTS)
  40.         
  41.             ' Set the star's colour
  42.             glColor3f (intensity#, intensity#, intensity#)
  43.             ' This is a form of the glColor command.
  44.             ' The last 2 characters tell us:
  45.             '   3 = Takes a 3D vector
  46.             '   F = Floating point values   
  47.             ' 3 parameters are: Red component, Blue component, Green component
  48.  
  49.             glVertex3fv (stars#(i))
  50.         glEnd ()
  51.     next
  52.     SwapBuffers ()
  53.     WaitTimer (20)
  54. wend
  55.